package com.gxuwz.lhleavesys.web.servlet;

import com.gxuwz.lhleavesys.context.AppContext;
import com.gxuwz.lhleavesys.entity.College;
import com.gxuwz.lhleavesys.service.CollegeService;
import com.gxuwz.lhleavesys.service.impl.CollegeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
*
* */
@WebServlet("/CollegeServlet")
public class CollegeServlet extends BaseLHServlet {

    // private String view_department = "/WEB-INF/views/user/user_department.jsp";
    // private String view_add = "/WEB-INF/views/user/user_addDepartment.jsp";
    // private String view_edit = "/WEB-INF/views/user/user_editDepartment.jsp";

    @Autowired
    private CollegeService collegeService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.collegeService = (CollegeServiceImpl) AppContext.getBean("collegeServiceImpl",
                    CollegeServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if (action==null) {
            List<College> college = collegeService.getAllColleges();
            System.out.println(college);
            request.setAttribute("college", college);
            doDispatch(request, response, "/views/admin/collegeList.jsp");
        }
        else if (action.equals("add")) {
            College college = new College();
            college.setCollegeID(request.getParameter("collegeID"));
            college.setCollegeName(request.getParameter("collegeName"));
            boolean flag = collegeService.addCollege(college);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('添加成功'); window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
                doRedirect(request, response, "CollegeServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('添加失败，请稍后重试，或尝试重进系统');window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
                doRedirect(request, response, "CollegeList");
            }
        }
        else if (action.equals("delete")) {
            String ID = request.getParameter("collegeID");
            boolean flag =  collegeService.deleteCollege(ID);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('删除成功');window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
//              response.sendRedirect("/CollegeServlet");
                doRedirect(request, response, "CollegeServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('删除失败，请稍后重试，或尝试重进系统');window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
//              response.sendRedirect("/CollegeServlet");
                doRedirect(request, response, "CollegeServlet");
            }
        }
        else if (action.equals("edit")) {
            String id = request.getParameter("id");
            College college = new College();
            college.setCollegeID(request.getParameter("collegeID"));
            college.setCollegeName(request.getParameter("collegeName"));
            boolean flag = collegeService.updateCollege(college,id);
            if (flag) {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('修改成功');window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
                doRedirect(request, response, "CollegeServlet");
            } else {
                response.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('修改失败，请稍后重试，或尝试重进系统');window.location.href='/CollegeServlet';</script>";
                response.getWriter().write(jsCode);
                doRedirect(request, response, "CollegeServlet");
            }
        }
        else if (action.equals("viewadd")) { 
            doDispatch(request,response,"/views/admin/addCollege.jsp");
        }
        else if (action.equals("viewedit")) {
            String ID = request.getParameter("collegeID");
            College college= collegeService.getCollegeByCollegeID(ID);
            request.setAttribute("college", college);
            doDispatch(request, response, "/views/admin/editCollege.jsp");
        }
        else if (action.equals("query")) {
            List<College> college = collegeService.BlurQuery(request.getParameter("collegeName"));
            request.setAttribute("college", college);
            doDispatch(request, response, "/views/admin/collegeList.jsp");
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}